﻿using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Report.PlugIn;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.ComponentModel;

namespace ZJRM.KingdeeK3Cloud.PlugIn.KAndOSysSynLog
{
    [Description("金蝶和第三方同步日志报表插件")]
    [HotUpdate]
    public class ReportDFPlugInSyncLog : AbstractSysReportPlugIn
    {
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);
            if (e.BarItemKey.EqualsIgnoreCase("tb_deleteLog"))
            {
                this.View.ShowMessage(string.Format("确定是否删除3天前的同步记录？"),
                    MessageBoxOptions.YesNo, new Action<MessageBoxResult>(
                        (result) =>
                        {
                            if (result == MessageBoxResult.Yes)
                            {
                                try
                                {
                                    string sqlStr = string.Format($"/*dialect*/ DELETE  FROM dbo.T_Log_SyncLogs WHERE DATEDIFF(dd,FmodifyDate,GETDATE())>3");
                                    var isOk = DBServiceHelper.Execute(this.Context, sqlStr);
                                    if (isOk > 0)
                                    {
                                        this.View.ShowMessage("同步日志清理成功!");
                                    }
                                    else
                                    {
                                        this.View.ShowMessage("同步日志清理失败!" + isOk.ToString());
                                    }
                                }
                                catch (Exception ex)
                                {
                                    this.View.ShowErrMessage($"同步日志清理失败:{ex.Message}");
                                }
                            }
                        }
                    ));

            }
        }
    }
}
